﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using CRMS.Models;
using CRMS.Views;

namespace CRMS.Views.ClassroomMgnt
{
    public partial class frmClassroomInfo : Form
    {
        private ClassRoomRep cr = new ClassRoomRep();
        private int Position;
        public frmClassroomInfo()
        {
            InitializeComponent();
            this.Position = 0;
        }

        private void frmClassroomInfo_Load(object sender, EventArgs e)
        {
            // 加载窗体时绑定数据源
            dataGridView1.DataSource = cr.getAllClassroomInfos();
            //在教学楼列表框中加载所有的教学楼信息
            cboxBuildingName.DataSource = cr.getTeachingBuilding();
            cboxBuildingName.DisplayMember = "TeachingBuildingName";
            cboxBuildingName.ValueMember = "TeachingBuildingName";
           
           
        }

        private void cboxQueryStyle_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboxQueryStyle.Text == "关键词搜索")
            {
                this.lbBuildingName.Visible = false;
                this.cboxBuildingName.Visible = false;
            }
            else
            {
                //查询时绑定教学楼名称
                cboxBuildingName.DataSource = cr.getTeachingBuilding();
                cboxBuildingName.DisplayMember = "TeachingBuildingName";
                cboxBuildingName.ValueMember = "TeachingBuildingName";
                this.lbBuildingName.Visible = true;
                this.cboxBuildingName.Visible = true;

            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtCID.ReadOnly = true;
            txtFloor.ReadOnly = true;
            tsbtnSave.Enabled = false;
            cboxBuildingName.Enabled = false;
            if (dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value == null)
            {
                return;
            }
            else
            {
                cboxTBName.Text = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtFloor.Text = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                txtCID.Text = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
            }
        }

        private void btnRetrive_Click(object sender, EventArgs e)
        {
            string tbname = cboxBuildingName.Text.Trim();
            string cid =txtClassroomId.Text.Trim();
            if (cboxQueryStyle.Text == "目录搜索")
            {
                if (tbname == string.Empty || cid == string.Empty)
                {
                    MessageBox.Show("请输入完整的查询条件！");
                }
                else
                {
                    int classroomId = Convert.ToInt32(cid);
                    dataGridView1.DataSource = cr.getCRMByCIDAndBName(classroomId, tbname);
                    if (dataGridView1.RowCount == 0)
                        MessageBox.Show("抱歉，未搜索到你要找的记录！");
                }
            }
            else
            {
                if (cid == string.Empty)
                {
                    MessageBox.Show("请输入完整的查询条件！");
                }
                else
                {
                    dataGridView1.DataSource = cr.getCRMByBCID(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                        Convert.ToInt32(cid));
                    if (dataGridView1.RowCount == 0)
                        MessageBox.Show("抱歉，未搜索到你要找的记录！");
                }
            }
        }

        private void tsbtnAdd_Click(object sender, EventArgs e)
        {
            txtClassroomId.Text = String.Empty;
            txtFloor.Text = String.Empty;
            SetDefualt();
        }

        private void SetDefualt()
        {
            //添加、修改时绑定教学楼名称
            cboxTBName.DataSource = cr.getTeachingBuilding();
            cboxTBName.DisplayMember = "TeachingBuildingName";
            cboxTBName.ValueMember = "TeachingBuildingId";
            cboxTBName.Enabled = true;
            txtFloor.ReadOnly = false;
            txtCID.ReadOnly = false;
            tsbtnSave.Enabled = true;
        }

        private void tsbtnSave_Change(object sender, EventArgs e)
        {
           
        }
        private void tsbtnSave_Click(object sender, EventArgs e)
        {
            Classroom classroom = new Classroom();
            string cid = txtCID.Text.Trim();
            string tbid = ((TeachingBuilding)cboxTBName.Items[cboxTBName.SelectedIndex]).TeachingBuildingId;
            string floor = txtFloor.Text.Trim();
            if (cid == string.Empty || tbid == string.Empty || floor == string.Empty)
            {
                MessageBox.Show("添加字段不能为空,请仔细检查！");
            }
            else
            {
                if (cr.getClassroomByBIDAndCID(tbid, Int32.Parse(cid)) != null)
                {
                    MessageBox.Show("教室已经存在！");
                }
                else
                {
                    classroom.ClassroomId = Int32.Parse(cid);
                    classroom.TeachingBuildingId = tbid;
                    classroom.Floor = floor;
                    cr.Add(classroom);
                    cboxTBName.Enabled = false;
                    txtFloor.ReadOnly = true;
                    txtCID.ReadOnly = true;
                    cr.Save();
                }
            }
            this.frmClassroomInfo_Load(sender,e);
            
        }

        private void tsbtnModify_Click(object sender, EventArgs e)
        {
            SetDefualt();
            tsbtnSave.Click -= new EventHandler(this.tsbtnSave_Click);
            tsbtnSave.Click += new EventHandler(this.tsbtnSave_Change);
        }

        private void tsbtnDelete_Click(object sender, EventArgs e)
        {
            Classroom classroom = cr.getClassroomByBNameAndCID(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                Int32.Parse(dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString()));
            cr.Delete(classroom);
            cr.Save();
            this.frmClassroomInfo_Load(sender, e);
            //删除教室信息后将文本框中的信息清空
            cboxTBName.Text = string.Empty;
            txtFloor.Text = string.Empty;
            txtCID.Text = string.Empty;
        }

        private void tsbtnFirstRecord_Click(object sender, EventArgs e)
        {

            if (dataGridView1.RowCount != 0)
            {
                if (this.Position != 0)
                {

                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.dataGridView1.Rows[0].Selected = true;
                    this.Position = 0;
                }
                    
            }
        }

        private void tsbtnUpRecord_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount != 0)
            {
                if (this.Position > 0)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.Position--;
                    this.dataGridView1.Rows[this.Position].Selected = true;
                    if (this.Position == 0)
                        MessageBox.Show("当前已是第一条记录！");
                }
            }
        }

        private void tsbtnDownRecord_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount != 0)
            {
                if (this.Position <dataGridView1.Rows.Count - 1)
                {
                    foreach (DataGridViewRow dgvr in this.dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.Position++;
                    this.dataGridView1.Rows[this.Position].Selected = true;
                    if (this.Position == dataGridView1.Rows.Count - 1)
                        MessageBox.Show("当前已是最后一条记录！");
                }
            }
        }

        private void tsbtnLastRecord_Click(object sender, EventArgs e)
        {
            if (dataGridView1.RowCount != 0)
            {
                if(this.Position != dataGridView1.Rows.Count-1)
                {
                    foreach (DataGridViewRow dgvr in dataGridView1.Rows)
                    {
                        dgvr.Selected = false;
                    }
                    this.dataGridView1.Rows[dataGridView1.Rows.Count-1].Selected = true;
                    this.Position = dataGridView1.Rows.Count-1;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (frmMain.fm.CheckForm("frmTcUseClassroomInfo"))
            {
                return;
            }
            else
            {
                frmTcUseClassroomInfo tcherUserCRoom =
                    new frmTcUseClassroomInfo(Int32.Parse(dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString()), 
                                                          dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString());
                tcherUserCRoom.MdiParent = frmMain.fm;
                this.Close();
                tcherUserCRoom.Show();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (frmMain.fm.CheckForm("frmClassroomToUse"))
            {
                return;
            }
            else
            {
                frmClassroomToUse cRoomToUse = new frmClassroomToUse(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString(),
                    Int32.Parse(dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString()));
                cRoomToUse.MdiParent = frmMain.fm;
                this.Close();
                cRoomToUse.Show();
            }
        }
    }
 }
 

    

